package com.gm.wholesale.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.util.timeUtils.DateTimeUtils;
import com.gm.commonlib.views.dialog.CustomDialog;
import com.gm.commonlib.views.popupwindow.Popupwindow2;
import com.gm.wholesale.R;
import com.gm.wholesale.module.user.UserDataManger;
import com.gm.wholesale.util.AppUtil;

public class HeaderView extends LinearLayout {
    public ImageView mLogoImage;
    private TextView mTimeTv;
    private TextView mUserNameTv;
    private Context mContext;
    public TextView backTv;
    private LinearLayout titleBackLin;

    private ImageView mUserPicterImg;


    public HeaderView(Context context) {
        super(context);
        initView(context);
    }

    public HeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_title, this);
        mLogoImage = findViewById(R.id.titleLeftImg);
        mUserPicterImg=findViewById(R.id.userPicterImg);
        backTv = findViewById(R.id.backTv);
        mTimeTv = findViewById(R.id.timeTv);
        mUserNameTv = findViewById(R.id.titleUserNameTv);
        initView();
        titleBackLin = findViewById(R.id.titleBackLin);
        mUserNameTv.setOnClickListener(view -> showExitAppDialog(view));
        mUserPicterImg.setOnClickListener(view -> showExitAppDialog(view));
    }

    private void showExitAppDialog(View view) {
        View mainView = LayoutInflater.from(mContext).inflate(R.layout.view_pop_exitapp, null);
        mainView.setOnClickListener(view1 -> {
            CustomDialog dialog = new CustomDialog(mContext);
            dialog.hiddenTitle();
            dialog.setMessage("您确定要退出吗?");
            dialog.setLeftText("取消");
            dialog.setRightText("确定");
            dialog.setOnRightListener((dialogInterface, i) -> AppUtil.startLoginAct(getContext()));
            dialog.show();
        });

        Popupwindow2.showPopView((Activity) mContext, view, mainView);
    }

    public void showBackView() {
        titleBackLin.setVisibility(VISIBLE);
    }

    public void hidBackView() {
        titleBackLin.setVisibility(GONE);
    }

    public void setUserName(String userName) {
        mUserNameTv.setText(userName);
    }

    public void initView() {
        String times = DateTimeUtils.formatDateTime(System.currentTimeMillis(), DateTimeUtils.SHORT_DATETIME_FORMAT_STRING);
        mTimeTv.setText(times + "  " + DateTimeUtils.getWeekTime(times));
        setUserName(UserDataManger.newInstance().getUserName());
    }

    public void setOnLogoClickListener(OnClickListener logoClickListener) {
        mLogoImage.setOnClickListener(logoClickListener);
    }

    public void setOnBackClickListener(OnClickListener logoClickListener) {
        hidBackView();
        backTv.setOnClickListener(logoClickListener);

    }

}

